const chalk = require('chalk');
const isProduction = process.env.NODE_ENV === 'production';
const moduleName = process.env.MODULE_NAME || 'demo';
const project = process.env.VUE_APP_PROJECT || 'pcact';
let pages = {};
const externals = {
	vue: "Vue",
	"vue-router": "VueRouter",
	vuex: "Vuex",
	axios: "axios",
	'element-ui': 'ELEMENT'
};

// 首页cdn加载
const cdn = {
	css: [
		"http://ynimg.zx093.cn/element-ui2.15.5/lib/theme-chalk/index.css"
	],
	js: [
		"http://ynimg.zx093.cn/vue2.6.11/vue.min.js",
		"http://ynimg.zx093.cn/vue-router2.8.1/vue-router.min.js",
		"http://ynimg.zx093.cn/vuex3.1.2/vuex.min.js",
		"http://ynimg.zx093.cn/axios0.21.1/axios.min.js",
		"http://ynimg.zx093.cn/element-ui2.15.5/lib/index.js"
	]
};

// 总的页面
const PAGES = require('./build/pages');
//组装公共打包
for (const basename in PAGES) {
	if (Object.prototype.hasOwnProperty.call(PAGES, basename)) {
		PAGES[basename].chunks = [
			'chunk-vue',
			'chunk-vendors',
			'chunk-common',
			`${basename}`
		];
		PAGES[basename].cdn = isProduction ? cdn : {};
	}
}

if (isProduction) {
	// 构建模块的名称
	if (!PAGES[moduleName]) {
		console.log(chalk.red(`${moduleName}模块不存在`));
		process.exit();
	}
	pages[moduleName] = PAGES[moduleName]
} else {
	pages = PAGES;
}
module.exports = {
	pages,
	publicPath: isProduction ? './' : '/',           //  解决打包之后静态文件路径404的问题
	outputDir: isProduction ? `dist/${project}/${moduleName}` : 'dist',        //  打包后的文件夹名称，默认dist
	devServer: {
		open: true, // 是否自动打开浏览器页面
		openPage: `experience/index.html#/TY`,
		host: "0.0.0.0",
		// YNGP = "http://app-test.zx093.cn/cjapp";
		proxy: {
			'/visualserver': {
				target: 'http://vweb-test.zx093.com',
				ws: true,
				changeOrigin: true
			},
			'/': {
				target: 'http://client-test.zx093.com',
				ws: true,
				changeOrigin: true
			}
		}
	},
	productionSourceMap: isProduction ? false : true,
	css: {
		loaderOptions: {
			// 设置 scss 公用变量文件
			sass: {
				prependData: `@import "~~@/assets/css/common.scss";`
			},
			css: {}
		}
	},
	chainWebpack(config) {
		config.when(isProduction ? true : false, config => {
			config.optimization.minimizer('terser').tap(options => {
				options[0].terserOptions.compress.drop_console = true;
				return options
			})
		})
	},
	//线上打包使用cdn加载
	configureWebpack: {
		externals: isProduction ? externals : {}
	}
};